#!/usr/bin/perl -w
#
# ubconfig -f <full_path>/ubroker.properties -v > ubconf.out
# search for any "VALIDATION MESSAGE" returned and correct the problem
#
# dzn:
# ERROR: portNumber=45576 specified for UBroker.AS.devcbf93qxoapp,
# UBroker.AS.devcbf93uiapp
# WARNING: UBroker.AS.QADFdevcbf93app PROPATH= references nonexistant
# directory /qad/projects/whitedev/cbf/sbx/gen/blf/liBB
# WARNING: /qad/progress/dlc102b01/properties/ubroker.properties-sav
# contains
# [Adapter]
#    actionalEnabled=0
#    actionalGroup=OpenEdge
#    allowRuntimeUpdates=0
#    appServerKeepaliveCapabilities=denyClientASK,denyServerAS
# and these items are missing from
# /qad/web/server/progress/10/coli43.cbf93.45599

use strict;
use Readonly;

# constant variables
my Readonly $EMPTY_STR = q();

# global variables
my $ubroker_file
        = '/cygdrive/d/workspace/script_mzq/qad/coli43.93.47399';
my $ubroker_template
        = '/cygdrive/d/workspace/script_mzq/qad/ubroker.properties-sav';
my %sections = ();

sub check_port {}

sub check_propath_dir {}

sub check_log_file_dir {}

sub check_unused_properties {}

sub get_sections {}

# read ubroker file and template from DLC
sub read_ubroker {
    my ( $ubroker_file_path ) = @_;
    open UBROKER_FILE, "<", "$ubroker_file_path" or die $!;
    for my $line (<UBROKER_FILE>) {
        my $current_section_type = $EMPTY_STR;
        my $current_property_ref = $EMPTY_STR;

        # if reading a new section like [Adapter]
        if ($line =~ /^\[([^\.]*).*\]$/) {
            my $current_section_type = $1;
            if (! defined $sections{ $current_section_type }) {
                $sections{ $current_section_type } = $current_property_ref;
            }
        }

        # if reading a property line
        elsif ($line !~ /^\s\+$/) {
            my %property_value = ();
            if ($current_property_ref == $EMPTY_STR) {
                $current_property_ref = \%property_value;
            }
            if ($line =~ /^\s\*(.*)=(.*)\s\*$/) {
                my $property_name  = $1;
                my $property_value = $2;
                if (! defined $$current_property_ref{ $property_name }) {
                    $$current_property_ref{ $property_name }
                            = $property_value;
                }
            }
        }
    }

    # print test
    foreach my $key_section_type (keys %sections) {
        my $value_property_ref = $sections{ $key_section_type };
        printf "[$key_section_type]:";
        foreach my $key_property_name (keys %$value_property_ref) {
            my $value_property_value
                    = $$value_property_ref{ $key_property_name };
            printf "  $key_property_name=$value_property_value";
        }
    }
    close UBROKER_FILE;
}

# main block
read_ubroker($ubroker_file);
